package com.ququ.syb.entrance.service;

import com.ququ.common.result.Result;
import com.ququ.syb.common.constant.EncryptTypeConstant;
import com.ququ.syb.common.constant.ResultStatusConstant;
import com.ququ.syb.common.pojo.WithdrawOrder;
import com.ququ.syb.entrance.fegin.WithdrawBusinessFegin;
import com.ququ.syb.entrance.po.RequestData;
import com.ququ.syb.entrance.vo.SybResult;
import com.ququ.syb.entrance.vo.SybResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WithdrawService {

    private Logger logger = LoggerFactory.getLogger(WithdrawService.class);
    @Autowired
    private WithdrawBusinessFegin withdrawBusinessFegin;
    @Autowired
    private SecurityService securityService;

    public SybResult withdraw(RequestData<String> requestData) {
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1, "加密方式不正确", securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String, Object> dataMap = securityService.getParametersMap(requestData);
        String accountid = requestData.getAccountid();
        String appcode = requestData.getAppcode();
        if (!dataMap.containsKey("amount") || dataMap.get("amount")==null )
            return new SybResult(-1,"缺少提现金额参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Long amount = Long.parseLong(dataMap.get("amount").toString());
        if (!dataMap.containsKey("outorderno") || dataMap.get("outorderno")==null )
            return new SybResult(-1,"缺少外部订单号参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String outorderno = dataMap.get("outorderno").toString();
        String tradenum = dataMap.containsKey("tradenum")?dataMap.get("tradenum").toString():null;
        if (!dataMap.containsKey("paychannelid") || dataMap.get("paychannelid")==null )
            return new SybResult(-1,"缺少通道号参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String paychannelid = dataMap.get("paychannelid").toString();
        Integer withdrawtype = dataMap.containsKey("withdrawtype")?Integer.parseInt(dataMap.get("withdrawtype").toString()):0;
        if (!dataMap.containsKey("callbackurl") || dataMap.get("callbackurl")==null )
            return new SybResult(-1,"缺少应用回调提现地址参数",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String callbackurl = dataMap.get("callbackurl").toString();

        Result<WithdrawOrder> withdrawResult = withdrawBusinessFegin.withdraw(accountid,tradenum,outorderno,amount,paychannelid,appcode,withdrawtype,callbackurl);
        if (withdrawResult.getStatus() == ResultStatusConstant.SUCCESS) {
            WithdrawOrder withdrawOrder = withdrawResult.getData();
            Map<String,Object> map = new HashMap<>();
            map.put("tradenum",withdrawOrder.getTradenum());
            map.put("commission",withdrawOrder.getCustomercommission());
            map.put("outorderno",withdrawOrder.getOutordernum());
            map.put("amount",withdrawOrder.getAmount());
            map.put("realamount",withdrawOrder.getAmount()-withdrawOrder.getCustomercommission());
            return securityService.buildSybResult(map, requestData);
        }else
            return new SybResult(-1,withdrawResult.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
    }

}
